import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/utils/date_time_utils.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/widgets/circular.dart';
import 'package:sports_new/widgets/base/qkd_image.dart';
import 'package:sports_new/widgets/base/qkd_text.dart';

import '../../../entity/ai/chart_match_entity.dart';
import '../../../utils/toast_utils.dart';
import '../../../utils/user.dart';

class ChartMatchInfo extends StatefulWidget {
  const ChartMatchInfo({super.key, required this.info});

  final ChartMatchEntity info;

  @override
  State<ChartMatchInfo> createState() => _ChartMatchInfoState();
}

class _ChartMatchInfoState extends State<ChartMatchInfo> {
  ChartMatchEntity get info => widget.info;
  String get league {
    if(info.leagueName.hasValue && info.matchTime.hasValue){
      return "${info.leagueName} ${DateTimeUtils.formatDateString(info.matchTime!, 'MM-dd HH:mm')}";
    }else if(info.leagueName.hasValue){
      return info.leagueName!;
    }else if(info.matchTime.hasValue){
      return DateTimeUtils.formatDateString(info.matchTime!, 'yyyy-MM-dd HH:mm:ss');
    }else{
      return "";
    }
  }
  String get score90 {
    if (info.homeScore.hasValue && info.guestScore.hasValue) {
      return "${info.homeScore}-${info.guestScore}";
    } else {
      return "VS";
    }
  }

  String get half {
    if (info.homeScoreHalf.hasValue && info.guestScoreHalf.hasValue) {
      return "半 ${info.homeScoreHalf}-${info.guestScoreHalf}";
    } else {
      return "";
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        if(league.hasValue)
        GestureDetector(
          onTap: () {
            if(info.leagueId.hasValue) {
              Get.toNamed(
                Routes.leagueDetail,
                arguments: info.leagueId, parameters: {"type":"0"}
              );
            }
          },
          child: QkdText(league,
            fontSize: 12,color: Colours.grey66,
          ).paddingOnly(bottom: 12),
        ),
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () => Get.toNamed(Routes.zqMatchDetail,arguments: info.matchId),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Expanded(
                child: team(info.homeLogo, info.homeName, info.homeId)
              ),
              score(),
              Expanded(
                child: team(info.guestLogo, info.guestName, info.guestId)
              )
            ],
          ),
        ),
      ],
    );
  }

  Widget score() {
    return Column(
      children: [
        QkdText(score90,
            fontSize: 32,
            fontWeight: FontWeight.w500,
            fontFamily: "Din"
        ),
        if(half.hasValue)
        QkdText(half,
            fontSize: 12,
            color: Colours.grey66,
            fontFamily: "Din"
        ),
        GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: (){
            if(!info.matchId.hasValue) return;
            if (User.isFollow(info.matchId ?? 0)) {
              Utils.onEvent('bsxq', params: {'bsxq': 11});
              User.unFollow(info.matchId ?? 0).then((value) {
                if (User.isFollow(info.matchId ?? 0)) {
                  ToastUtils.showDismiss("取消关注失败");
                } else {
                  ToastUtils.showDismiss("取消关注成功");
                }
                setState(() {});
              });
            } else {
              Utils.onEvent('bsxq', params: {'bsxq': 1});
              User.follow(info.matchId ?? 0).then((value) {
                if (User.isFollow(info.matchId ?? 0)) {
                  ToastUtils.showDismiss("关注成功");
                } else {
                  ToastUtils.showDismiss("关注失败");
                }
                setState(() {});
              });
            }
          },
          child: Container(
            margin: const EdgeInsets.only(top: 12),
            width: 76,height: 24,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              border: Border.all(
                color: User.isFollow(info.matchId ?? 0)?Colours.greyCC:Colours.main,
                width: 0.5
              ),
              borderRadius: BorderRadius.circular(24.5)
            ),
            child: QkdText(User.isFollow(info.matchId ?? 0)?"已关注":"关注比赛",
              fontSize: 13,
              color: User.isFollow(info.matchId ?? 0)?Colours.greyCC:Colours.main,
              needCenter: true
            )
          ),
        )
      ],
    );
  }

  Widget team(String? logo, String? name, int? id) {
    return Column(
      children: [
        QkdImage(imageUrl: logo, width: 52, height: 52),
        Container(height: 4),
        SizedBox(
          height: 30*1.4,
          child: QkdText(name,textAlign: TextAlign.center,
            fontSize: 15, fontWeight: FontWeight.w500,maxLines: 2
          ).paddingSymmetric(horizontal: 24),
        )
      ],
    );
  }
}